<?php 
/**
 * @version        $Id$
 * @author         master@xuewl.com
 * @copyright      Copyright (c) 2007 - 2014, Chongqing xuewl Information Technology Co., Ltd.
 * @link           http://www.xuewl.com
**/
namespace Special\Controller;
use \Admin\Controller\InitController;
/* 专题后台管理 */
class ContentController extends InitController {
	public function _initialize() {
		parent::_initialize();
		$this->db = D('SpecialContent');
		$this->data_db = D('SpecialCData');
		$this->special_db = D('Special');
	}

	/* 管理信息 */
	public function manage($specialid = 0) {
		$specialid = (int) $specialid;
		$page = max(1, (int) I('page'));
		$sqlMap = array();
		$sqlMap['specialid'] = $specialid;
		$count = $this->db->where($sqlMap)->count();
		$infos = $this->db->where($sqlMap)->page($page, 10)->select();
		$pages = page($page, 10);
		include $this->admin_tpl('content_manage');

	}

	/* 添加信息 */
	public function add() {
		$specialid = I('specialid', 0, 'intval');		
		if (submitcheck('dosubmit')) {
			$specialinfo = $this->special_db->getById($specialid);
			if(!$specialinfo) $this->error(L('special_not_exist'));
			$info = (array) $_POST['info'];
			$info = $this->check($info);
			//处理外部链接情况
			$info['specialid'] = $specialid;
			if ($info['islink']) {
				$info['url'] = $info['linkurl'];
				$info['isdata'] = 0;
			} else {
				$info['isdata'] = 1;
			}			
			$contentid = $this->db->update($info);
			if (!$contentid) {
				$this->error($this->db->getError());
			}
			/* 处理附加信息 */
			$hitsid = 'special-c-'.$info['specialid'].'-'.$contentid;
			D('Hits')->add(array('hitsid'=>$hitsid));
			if ($info['isdata']) {
				$data = (array) $_POST['data'];
				$data['id'] = $contentid;
				$this->data_db->add($data);
				$url = U('Special/Index/content', array('id' => $contentid));
				$this->db->update(array('id' => $contentid,'url'=>$url), FALSE);
			}
			$forward = (isset($_POST['dosubmit'])) ? 'javascript:window.close();close_dialog();'  : HTTP_REFERER;
			$this->success(L('add_special_success'), $forward);
		} else {
			$form = new \Common\Library\form();
            /* 加载可用风格 */
            $template_list = template_list();
            foreach ($template_list as $k=>$v) {
                $template_list[$v['dirname']] = $v['name'] ? $v['name'] : $v['dirname'];
                unset($template_list[$k]);
            }
			include $this->admin_tpl('content_add');
		}
	}

	/* 专题编辑 */
	public function edit() {
		$id = I('id', 0, 'intval');
		$info = $this->db->getById($id);
		$data = array();
		if (!$info) $this->error('信息不存在');
		if (submitcheck('dosubmit')) {
			$info = (array) $_POST['info'];
			$info = $this->check($info);
			//处理外部链接情况
			$info['id'] = $id;
			if ($info['islink']) {
				$info['url'] = $info['linkurl'];
				$info['isdata'] = 0;
			} else {
				$info['isdata'] = 1;
			}			
			$result = $this->db->update($info);
			if (!$result) {
				$this->error($this->db->getError());
			}
			/* 处理附加信息 */
			if ($info['isdata']) {
				$data = (array) $_POST['data'];
				$data['id'] = $id;
				$data = $this->check($data);
				$this->data_db->save($data);
				$url = U('Special/Index/content', array('id' => $id));
				$this->db->update(array('id' => $id,'url'=>$url), FALSE);
			}
			$forward = (isset($_POST['dosubmit'])) ? 'javascript:window.close();close_dialog();'  : HTTP_REFERER;
			$this->success('信息编辑成功', $forward);
		} else {
			if ($info['isdata']) {
				$data = $this->data_db->getById($id);
			}
			$form = new \Common\Library\form();
            /* 加载可用风格 */
            $template_list = template_list();
            foreach ($template_list as $k=>$v) {
                $template_list[$v['dirname']] = $v['name'] ? $v['name'] : $v['dirname'];
                unset($template_list[$k]);
            }
            include $this->admin_tpl('content_edit');
		}	
	}

	/* 信息删除 */
	public function delete() {
		$ids = (array) I('param.id');
		if ($ids) {
			foreach ($ids as $id) {
				$this->db->delete($id);
				$this->data_db->delete($id);
			}
			$this->success('操作成功');
		} else {
			$this->error('参数错误');
		}
	}

	/* 心思排序 */
	public function public_listorder() {
		$listorders	= (array) $_POST['listorder'];
		if($listorders) {
			foreach ($listorders as $id => $listorder) {
				if(!is_numeric($id) || !is_numeric($listorder)) continue;
				$this->db->where(array('id' => $id))->setField('listorder', $listorder);
			}
			$this->success(L('update_special_success'));
		} else {
			$this->error('参数错误');
		}
	}

	/* 专题重复检测 */
	public function public_check_special($clientid = 'title') {
		if ($clientid == 'title') {
			$title = trim(I('title'));
			if(!empty($title)) {
				$sql = array('title' => $title);
				$id = (int) I('id');
				if ($id > 1) $sql['id'] = array("NEQ", $id);
				if ($this->db->where($sql)->count()) {
					$this->error(L('special_exist'));
				}
			}
		}
		$this->success('专题可用');
	}

	/* 初始化数据 */
	private function check($data) {
		/* 外部链接 */
		$data['islink'] = $data['islink'] ? intval($data['islink']) : 0;
		/* 标题颜色 */
		$data['style'] = '';
		if ($data['style_color']) {
			$data['style'] .= 'color:#00FF99;';
		} 
		if ($data['style_font_weight']) {
			$data['style'] .= 'font-weight:bold;';
		}
		/* 截取简介 */
		if ($_POST['add_introduce'] && $data['description']=='' && !empty($content)) {
			$content = stripslashes($content);
			$introcude_length = intval($_POST['introcude_length']);
			$data['description'] = str_cut(str_replace(array("\r\n","\t"), '', strip_tags($content)),$introcude_length);
		}
		/* 提取缩略图 */
		if (isset($_POST['auto_thumb']) && $data['thumb'] == '' && !empty($content)) {
				$content = stripslashes($content);
				$auto_thumb_no = intval($_POST['auto_thumb_no']) - 1;
				if (preg_match_all("/(src)=([\"|']?)([^ \"'>]+\.(gif|jpg|jpeg|bmp|png))\\2/i", $content, $matches)) {
					$data['thumb'] = $matches[3][$auto_thumb_no];
				}
		}
		unset($data['style_color'], $data['style_font_weight']);
		return $data;
	}
}